package com.portal.common.thread.example.part3;

/**
 * 线程安全：都是由全局变量及静态变量引起的
 * 1、若对全局变量及静态变量只有读操作没有写操作，则线程安全；若有写操作，则线程不安全。
 */

class Ticket implements Runnable{
    private int ticketNums = 100;

    @Override
    public void run() {
        while (true){
            if (ticketNums > 0){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                System.out.println(Thread.currentThread().getName() + "正在卖第" + "张票"+ ticketNums--);
            }
        }
    }
}
public class ThreadSafe {
    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        Thread window1 = new Thread(ticket, "窗口1");
        Thread window2 = new Thread(ticket, "窗口2");
        Thread window3 = new Thread(ticket, "窗口3");
        window1.start();
        window2.start();
        window3.start();
    }
}
